import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { terser } from 'rollup-plugin-terser'
import compression from 'vite-plugin-compression'
import vue from '@vitejs/plugin-vue'
import babel from '@rollup/plugin-babel'

import VueSetupExtend from 'vite-plugin-vue-setup-extend'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    VueSetupExtend(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    {
      postcss: {
        plugins: [
          require('postcss-pxtorem')({
            rootValue: 1, //1px = 1rem
            propList: ['*']
          })
        ]
      }
    },
    babel({
      babelHelpers: 'bundled',
      presets: [
        [
          '@babel/preset-env',
          {
            targets: '> 0.25%, not dead'
          }
        ]
      ]
    })

    //打包开启gzip压缩，需要后端支持
    // compression({
    //   verbose: true,
    //   disable: false,
    //   threshold: 10240, // 压缩阈值，小于这个值的文件将不会被压缩（默认10k）
    //   algorithm: 'gzip', // 压缩算法
    //   ext: '.gz', // 压缩后文件的后缀
    //   deleteOriginFile: true, // 是否删除原文件
    //   // 包括/排除路径
    //   include: /\.(js|css|html)$/,
    //   exclude: /node_modules/
    // })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      '~@': fileURLToPath(new URL('./src', import.meta.url)),
      '/@/': fileURLToPath(new URL('./src', import.meta.url))
    }
  },

  server: {
    open: true,
    host: '0.0.0.0',
    proxy: {
      // '/server/zfcjz': {
      //   target: 'http://192.168.10.152:15071/zfcjz',
      //   changeOrigin: true,
      //   rewrite: (path) => path.replace(/^\/server\/zfcjz/, '')
      // },
      '/znjszd3': {
        target: 'http://192.168.9.101:28080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/znjszd3/, '/znjszd3')
      }
    }
  },
  build: {
    outDir: 'znjszd3pcvue3.0.1',
    // outDir: 'D:/nginx/nginx-1.26.1/html',
    target: 'es2015',
    cssTarget: 'chrome80',
    assetsInlineLimit: 5120, //小于5kb的导入或引用资源将内联为 base64 编码
    minify: 'terser',
    terserOptions: {
      compress: {
        // 去除所有的 `console` 语句
        drop_console: true,
        // 去除所有的 `debugger` 语句
        drop_debugger: true
      },
      output: {
        // 去除注释内容
        comments: true
      }
    }
    // rollupOptions: {
    //   plugins: [terser()],
    // },
  }
})
